#pragma once

#include <mutex>
#include <mutex>
#include <string>
#include <functional>
#include <condition_variable>

#include "Thread.h"
#include "EventLoop.h"
#include "noncopyable.h"

class EventLoopThread : public noncopyable
{
public:
    using ThreadInitCallback = std::function<void(EventLoop*)>;

    EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback(),
        const std::string& name = std::string());
    ~EventLoopThread();

    // 开启事件循环并返回loop_
    EventLoop* startLoop();

private:
    void threadFunc();

    EventLoop* loop_;
    bool exiting_;
    Thread thread_;
    std::mutex mutex_; // 保护loop_的修改
    std::condition_variable cond_; // loop_创建完成，用cond_来进行通知
    ThreadInitCallback callback_;  
};